package com.hnevc.xiaoming.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import com.hnevc.xiaoming.R;
import com.hnevc.xiaoming.adapter.ViewPagerAdapter;
import com.hnevc.xiaoming.fragment.Fragment1;
import com.hnevc.xiaoming.fragment.Fragment2;
import com.hnevc.xiaoming.fragment.Fragment3;

import java.util.ArrayList;
import java.util.List;

public class MyViewPager extends AppCompatActivity {

    private ViewPager viewPager;
    private List<View> dots;

    private int oldPosition = 0;// 记录上一次点的位置

    private int currentItem; // 当前页面

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
        initView();
        initData();
        initEvent();
    }

    private void initEvent() {


    }

    private void initData() {


    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.vp);

        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new Fragment1());
        fragmentList.add(new Fragment2());
        fragmentList.add(new Fragment3());

        dots = new ArrayList<>();
        dots.add(findViewById(R.id.dot_1));
        dots.add(findViewById(R.id.dot_2));
        dots.add(findViewById(R.id.dot_3));

        dots.get(0).setBackgroundResource(R.drawable.xiaoyuandian_null);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {

                //获取上一个位置，并且把点的状体设置成默认状体
                dots.get(oldPosition).setBackgroundResource(R.drawable.xiaoyuandian);
                //获取到选中页面对应的点，设置为选中状态
                dots.get(position).setBackgroundResource(R.drawable.xiaoyuandian_null);
                //下面是记录本次的位置，因为在滑动，他就会变成过时的点了
                oldPosition = position;
                //关联页卡
                currentItem = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(),fragmentList));
    }
}